# Load Packages -----------------------------------------------------------
library(stargazer)
library(cem)

# Load Data ---------------------------------------------------------------
# Set file path
setwd("")

load("drop_in_ocean_data.rdata")


# Analyses ----------------------------------------------------------------
# * By Race ---------------------------------------------------------------
# bivariate model
m_cjs_att_bivar <- lm(cjs_att ~ direct_exp*black,
                      data = cjs.df, weights = wts_bw_stk)
summary(m_cjs_att_bivar)

# model with controls
cjs_att_mod_race <- lm(cjs_att ~ direct_exp*black + rr_sc*black + 
                         peer.felony*black + age_sc*black + man*black + 
                         educ_sc*black + inc_sc*black + pid7_dem*black,
                       data = cjs.df, weights = wts_bw_stk)
summary(cjs_att_mod_race)


# * By Gender (Black) -----------------------------------------------------
# bivariate model
m_cjs_att_blk_bivar <- lm(cjs_att ~ direct_exp*man,
                          data = cjs.df, subset = black == 1,  weights = wts_bw_stk)
summary(m_cjs_att_blk_bivar)

# model with controls
cjs_att_mod_gender_blk <- lm(cjs_att ~ direct_exp*man + rr_sc*man +
                               peer.felony*man + age_sc*man + educ_sc*man + inc_sc*man + pid7_dem*man,
                             data = cjs.df, subset = black == 1,  weights = wts_bw_stk)
summary(cjs_att_mod_gender_blk)




# * By Gender (White) -----------------------------------------------------
# bivariate model
m_cjs_att_wht_bivar <- lm(cjs_att ~ direct_exp*man,
                          data = cjs.df, subset = black == 0,  weights = wts_bw_stk)
summary(m_cjs_att_wht_bivar)


# model with controls
cjs_att_mod_gender_wht <- lm(cjs_att ~ direct_exp*man + rr_sc*man +
                               peer.felony*man + age_sc*man + educ_sc*man + inc_sc*man + pid7_dem*man,
                             data = cjs.df, subset = black == 0,  weights = wts_bw_stk)
summary(cjs_att_mod_gender_wht)




# Tables A3 and A4 -------------------------------------------------------
stargazer(m_cjs_att_bivar, m_cjs_att_blk_bivar, m_cjs_att_wht_bivar,
          title = "Direct Experiences Effect Carceral State Attitudes by Race and Gender",
          dep.var.caption = "",
          dep.var.labels.include = FALSE,
          column.labels = c("", "Black", "White"),
          covariate.labels = c("Direct Experiences", "Experiences*Black", "Experiences*Man",
                               "Black", "Man"),
          order = c(1, 3,5,2,4),
          model.numbers = F,
          no.space = T, header = F,
          notes = c("OLS regression results with standard errors in parentheses. Analyses weighted. Measures scaled 0-1."), 
          notes.align = "l", 
          intercept.bottom = T, initial.zero = F, label = c("main_bivar"),
          type = "text",
          # out = c("tableA3.tex"),
          digits = 3, omit.stat = c("f", "adj.rsq"), df = F,
          align = T, star.char = c("*"), star.cutoffs = c(0.05))

stargazer(cjs_att_mod_race, cjs_att_mod_gender_blk, cjs_att_mod_gender_wht,
          title = "Direct Experiences Effect Carceral State Attitudes by Race and Gender",
          dep.var.caption = "",
          dep.var.labels.include = FALSE,
          column.labels = c("", "Black", "White"),
          covariate.labels = c("Direct Experiences", "Experiences*Black", "Experiences*Man",
                               "Black", "Man", "Black*Man",
                               "Racial Resentment", "Resentment*Black", "Resentment*Man",
                               "Peers with Felony Convictions", "Peers*Black", "Peers*Man",
                               "Age", "Age*Black", "Age*Man",
                               "Education", "Education*Black", "Education*Man",
                               "Income", "Income*Black", "Income*Man",
                               "Partisanship", "Partisanship*Black", "Partisanship*Man"),
          order = c(1, 10, 18,  2, 6, 14, 3, 11, 19, 4, 12, 20, 5, 13, 21, 7, 15, 22, 8, 16, 23),
          model.numbers = F,
          no.space = T, header = F,
          notes = c("OLS regression results with standard errors in parentheses. Analyses weighted. Measures scaled 0-1."), 
          notes.align = "l", 
          intercept.bottom = T, initial.zero = F, label = c("pred_var_RR"),
          type = "text",
          # out = c("tableA4.tex"),
          digits = 3, omit.stat = c("f", "adj.rsq"), df = F,
          align = T, star.char = c("*"), star.cutoffs = c(0.05))



# Table A5 --------------------------------------------------------------
# Create Quality IV -------------------------------------------------------
cjs.df$pol_quality <- factor(cjs.df$pol_quality,
                             labels = c("Positive", "Mixed", "Negative"))


# By Race -----------------------------------------------------------------
# * Specify Matching Weights ------------------------------------------------
# define variables
demogs <- c("pid3", "rr_sc", "peer.felony", "age", "man", "educ3", "inc")

# subset data
cem_dat <- na.omit(subset(cjs.df, 
                          select = c(demogs, "pol.stop","pol_quality", "cjs_att", "black", "wts_bw_stk",
                                     "pid7_dem", "age_sc", "educ_sc", "inc_sc")))

# define breaks for continuous items
cuts <- list(age = 4, inc = c(-.05, 2, 6, 11), peer.felony = c(-.05, .33, 1),
             rr_sc = 4) 

# create matched object
mat <- cem(treatment = "pol_quality", 
           data = cem_dat, 
           drop = c("cjs_att", "black", "wts_bw_stk",
                    "pid7_dem", "age_sc", "educ_sc", "inc_sc"),
           cutpoints = cuts)
mat


# * Estimate Model ----------------------------------------------------------
m_race <- lm(cjs_att ~ pol_quality*black + pol.stop*black + rr_sc*black + peer.felony*black + age_sc*black + 
               man*black + educ_sc*black + inc_sc*black + pid7_dem*black, 
             cem_dat, weights = mat$w)
summary(m_race)


# By Gender within Race -----------------------------------------------------------------
# * Specify Matching Weights ------------------------------------------------
# specify variables
demogs <- c("pid3", "rr_sc", "peer.felony", "man", "age", "educ3", "inc")

# Black respondents subset
cem_dat_black <- na.omit(subset(cjs.df,
                                black == 1,
                                select = c(demogs, "pol.stop","pol_quality", "cjs_att", "wts_bw_stk",
                                           "pid7_dem", "age_sc", "educ_sc", "inc_sc")))

# define breaks for continuous variables
cuts_blk <- list(age = c(17, 28, 40, 90), inc = c(-.05, 2, 6, 11), peer.felony = c(-.05, .33, 1), 
                 rr_sc = 4) 

# create matched object
mat_black <- cem(treatment = "pol_quality", 
                 data = cem_dat_black, 
                 drop = c("cjs_att", "man", "wts_bw_stk",
                          "pid7_dem", "age_sc", "educ_sc", "inc_sc"),
                 cutpoints = cuts_blk
)
mat_black

# White respondents subset
cem_dat_white <- na.omit(subset(cjs.df,
                                black == 0,
                                select = c(demogs, "pol.stop","pol_quality", "cjs_att", "wts_bw_stk",
                                           "pid7_dem", "age_sc", "educ_sc", "inc_sc")))

# define breaks for continuous variables
cuts_wht <- list(age = c(17, 28, 38, 56, 90), inc = c(-.05, 2, 5, 11), peer.felony = 2, 
                 rr_sc = 4) 

# create matched object
mat_white <- cem(treatment = "pol_quality", 
                 data = cem_dat_white, 
                 drop = c("cjs_att", "man", "wts_bw_stk",
                          "pid7_dem", "age_sc", "educ_sc", "inc_sc"),
                 cutpoints = cuts_wht
)
mat_white


# * Estimate Model ----------------------------------------------------------
# Black respondents
m_gend_blk <- lm(cjs_att ~ pol_quality*man + pol.stop*man + rr_sc*man + peer.felony*man + age_sc*man + 
                   educ_sc*man + inc_sc*man + pid7_dem*man, 
                 cem_dat_black, weights = mat_black$w)
summary(m_gend_blk)

# White respondents
m_gend_wht <- lm(cjs_att ~ pol_quality*man + pol.stop*man + rr_sc*man + peer.felony*man + age_sc*man + 
                   educ_sc*man + inc_sc*man + pid7_dem*man, 
                 cem_dat_white, weights = mat_white$w)
summary(m_gend_wht)



# * Table -----------------------------------------------------------------
stargazer(m_race, m_gend_blk, m_gend_wht,
          title = "Experience Quality's Effect on Carceral State Attitudes by Race and Gender",
          dep.var.caption = "",
          dep.var.labels.include = FALSE,
          column.labels = c("", "Black", "White"),
          covariate.labels = c("Unsafe or Unfair", "Unsafe or Unfair*Black", "Unsafe or Unfair*Man",
                               "Unsafe and Unfair", "Unsafe and Unfair*Black", "Unsafe and Unfair*Man",
                               "Black", "Man", "Black*Man",
                               "Police Stops (n)", "Stops*Black", "Stops*Man",
                               "Racial Resentment", "Resentment*Black", "Resentment*Man",
                               "Peers with Felony Convictions", "Peers*Black", "Peers*Man",
                               "Age", "Age*Black", "Age*Man",
                               "Education", "Education*Black", "Education*Man",
                               "Income", "Income*Black", "Income*Man",
                               "Partisanship", "Partisanship*Black", "Partisanship*Man"),
          order = c(1, 12, 22, 2, 13, 23, 3, 8, 18, 4, 14, 24, 5, 15, 25, 6, 16, 26, 7, 17, 27,
                    9, 19, 28, 10, 20, 29, 11, 21, 30),
          model.numbers = F,
          no.space = T, header = F,
          notes = c("OLS regression results with standard errors in parentheses. Analyses weighted. Measures scaled 0-1."), 
          notes.align = "l", 
          intercept.bottom = T, initial.zero = F, label = c("exp_quality"),
          type = "text",
          # out = c("tableA5.tex"),
          digits = 3, omit.stat = c("f", "adj.rsq"), df = F,
          align = T, star.char = c("*"), star.cutoffs = c(0.05))


